package com.adopapa.nezhacloud.authc.controller;

import java.util.Map;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.adopapa.nezhacloud.authc.domain.RoleInfo;
import com.adopapa.nezhacloud.authc.service.RoleInfoService;
import com.adopapa.nezhacloud.common.RestResponse;
import com.adopapa.nezhacloud.common.Tools;
import com.adopapa.nezhacloud.ddd.codegen.SqlGen;
import com.adopapa.nezhacloud.ddd.conf.AppConstants;
import com.adopapa.nezhacloud.ddd.controller.BaseController;

@RestController
@RequestMapping("/authc/roleinfo")
public class RoleInfoController extends BaseController {

	@Autowired
	private RoleInfoService roleInfoService;

	@SqlGen(name = { "权限管理", "角色管理", "分页查询" }, method = SqlGen.METHOD_GET, path = "/roleinfos/{pageNo}")
	@GetMapping(path = "/roleinfos/{pageNo}", produces = MediaType.APPLICATION_JSON_VALUE)
	public RestResponse getRoleInfoPage(@RequestHeader String authorization, //
			@PathVariable Integer pageNo, //
			@RequestParam(required = false) String roleName, //
			@RequestParam(required = false) String remark, //
			@RequestParam(required = false) Integer pageSize) {

		RoleInfo roleInfo = new RoleInfo();
		
		roleInfo.setRoleName(roleName);	
		roleInfo.setRemark(remark);	

		pageSize = Tools.empty(pageSize) ? AppConstants.DEFAULT_PAGE_SIZE : pageSize;

		Page<RoleInfo> page = roleInfoService.getRoleInfoPage(pageNo, pageSize, roleInfo);

		return RestResponse.ok(page);
	}

	@SqlGen(name = { "权限管理", "角色管理", "单个查询" }, method = SqlGen.METHOD_GET, path = "/roleinfo/{id}")
	@GetMapping(path = "/roleinfo/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
	public RestResponse getHappyDemo(@RequestHeader String authorization, //
			@PathVariable String id) {
		RoleInfo roleInfo = roleInfoService.getRoleInfoById(id);
		return RestResponse.ok(roleInfo);
	}

	@SqlGen(name = { "权限管理", "角色管理", "编辑信息" }, method = SqlGen.METHOD_POST, path = "/roleinfos")
	@PostMapping(path = "/roleinfos", produces = MediaType.APPLICATION_JSON_VALUE, //
			consumes = MediaType.APPLICATION_JSON_VALUE)
	public RestResponse saveRoleInfo(@RequestHeader String authorization, //
			@RequestBody Map<String, Object> params) {

		String id = getMapValue(params, "id", String.class);
		
		String roleName = getMapValue(params, "roleName", String.class);
		String remark = getMapValue(params, "remark", String.class);
	
		if(Tools.empty(roleName)) {
			throw new RuntimeException("角色名称不能为空");
	    }
	
		RoleInfo roleInfo = null;
		if (Tools.empty(id)) {
			roleInfo = new RoleInfo();
//			roleInfo.setCreateTime(Instant.now());
		} else {
			roleInfo = roleInfoService.getRoleInfoById(id);
			if (Tools.empty(roleInfo)) {
				throw new RuntimeException("指定id对应信息不存在");
			}
		}
		
		roleInfo.setRoleName(roleName);
		roleInfo.setRemark(remark);

		roleInfoService.saveRoleInfo(roleInfo);

		return RestResponse.ok();

	}

	@SqlGen(name = { "权限管理", "角色管理", "删除信息" }, method = SqlGen.METHOD_DELETE, path = "/roleinfo/{id}")
	@DeleteMapping(path = "/roleinfo/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
	public RestResponse deleteRoleInfo(@RequestHeader String authorization, //
			@PathVariable String id) {
		try {
			roleInfoService.deleteRoleInfo(id);
		} catch (Exception e) {
			logger.error(ExceptionUtils.getStackTrace(e));
		}
		
		return RestResponse.ok();
	}

}
